using System.Collections.Generic;
using Photon;
using UnityEngine;

public class ItemFunctions : Photon.MonoBehaviour
{
	public string m_NameObject;

	public bool m_Take;

	public bool m_ItDoor;

	public Rigidbody m_rigDor;

	public CarFunction s_CarFunct;

	public List<string> playerInCar;

	public bool m_ObjectIsKinematic;

	private GameObject playerOwner;

	public Rigidbody m_Rigidibody;

	private void Awake()
	{
		m_Rigidibody = GetComponent<Rigidbody>();
		if (base.gameObject.tag == "Item")
		{
			base.name = m_NameObject;
		}
		if (base.gameObject.tag == "Car")
		{
			s_CarFunct = GetComponent<CarFunction>();
		}
		if (m_ItDoor)
		{
			m_rigDor.centerOfMass = new Vector3(0f, 0f, 0f);
		}
	}

	private void Update()
	{
		if (PhotonNetwork.room != null && !PhotonNetwork.offlineMode && Manager.Instance.s_PlayerSetup != null && base.gameObject.tag == "Item" && !base.photonView.isMine)
		{
			m_Rigidibody.freezeRotation = true;
		}
	}

	[PunRPC]
	private void SitDownVechile(string playerName)
	{
		if (base.gameObject.tag == "Car")
		{
			s_CarFunct.SitDownCar(playerName);
		}
	}

	[PunRPC]
	private void ExitVechile(string playerName)
	{
		if (base.gameObject.tag == "Car")
		{
			s_CarFunct.ExitCar(playerName);
		}
	}

	public int PlayerInCarCurrent()
	{
		int num = 0;
		foreach (string item in s_CarFunct.playerInCar)
		{
			if (item != string.Empty)
			{
				num++;
			}
		}
		return num;
	}

	public void SitDownOrQuitInCar(bool active, bool driver, GameObject owner, int numberSkin)
	{
		if (driver)
		{
			m_Take = active;
			m_Rigidibody.isKinematic = false;
		}
		playerOwner = owner;
		if (base.gameObject.tag == "Car")
		{
			s_CarFunct.SitDownOrQuitInCar(active, driver);
		}
	}

	public void GetObject(GameObject owner)
	{
		m_Take = true;
		m_Rigidibody.useGravity = false;
	}

	private void OwnerShip(bool active)
	{
		if (active)
		{
			base.photonView.RequestOwnership();
		}
	}

	public void DropObject()
	{
		m_Take = false;
		m_Rigidibody.useGravity = true;
	}

	[PunRPC]
	public void DropObjectWhenDisconect()
	{
		m_Take = false;
		s_CarFunct.m_PlayerInCar = false;
	}

	public void SetObjectKinematick(bool active)
	{
		m_ObjectIsKinematic = active;
		m_Rigidibody.isKinematic = active;
		Debug.Log("ACTIVE " + active);
	}

	public void DataUpdate()
	{
		Debug.Log("DataUpdate");
		m_Rigidibody.isKinematic = m_ObjectIsKinematic;
		if (m_Take)
		{
			m_Rigidibody.useGravity = false;
		}
		else
		{
			m_Rigidibody.useGravity = true;
		}
		if (m_ObjectIsKinematic)
		{
			m_Rigidibody.isKinematic = true;
		}
		else
		{
			m_Rigidibody.isKinematic = false;
		}
	}

	public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
	{
		if (stream.isWriting)
		{
			stream.SendNext(m_Take);
			stream.SendNext(m_ObjectIsKinematic);
		}
		else
		{
			m_Take = (bool)stream.ReceiveNext();
			m_ObjectIsKinematic = (bool)stream.ReceiveNext();
			DataUpdate();
		}
	}

	private void OnDisable()
	{
		if (base.gameObject.tag == "Item" && PhotonNetwork.room != null && !PhotonNetwork.offlineMode)
		{
			RoomPropertyFunction.RemoveItemScore();
		}
		if (base.gameObject.tag == "Car" && playerOwner != null)
		{
			playerOwner.GetComponent<PlayerSetup>().OutWithCar(base.gameObject, m_Rigidibody.velocity);
		}
	}

	private void OnPhotonPlayerDisconnected(PhotonPlayer otherPlayer)
	{
		if (playerInCar.Contains(otherPlayer.name) && base.gameObject.tag == "Car")
		{
			for (int i = 0; i < s_CarFunct.m_maxPlayersInCar; i++)
			{
				if (playerInCar[i] == otherPlayer.name)
				{
					playerInCar[i] = string.Empty;
					if (i == 0)
					{
						s_CarFunct.m_PlayerInCar = false;
					}
					break;
				}
			}
		}
		if (base.gameObject.tag == "Car")
		{
			if (base.photonView.isMine && base.gameObject != null)
			{
				m_Take = false;
				s_CarFunct.m_PlayerInCar = false;
			}
			s_CarFunct.SortPlayer();
			OwnerShip(s_CarFunct.CarDriver());
		}
		if (base.gameObject.tag == "Item" && base.photonView.isMine && base.gameObject != null)
		{
			m_Take = false;
		}
	}
}
